package TCP3;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws Exception {
        System.out.println("服务端已启动^^");
        // 1.创建ServerSocket对象，同时为服务端注册端口
        ServerSocket serverSocket = new ServerSocket(8888);
        // 2.使用ServerSocket对象调用accept方法，等待客户端的连接请求
        Socket socket = serverSocket.accept();
        // 3.从socket通信管道中得到一个字节输入流
        InputStream is = socket.getInputStream();
        // 4.把原始的字节输入流包装成数据输入流
        DataInputStream dis = new DataInputStream(is);
        while(true){
            try{
                // 5.使用数据输入流读取客户端发送过来的消息
                String rs = dis.readUTF();
                System.out.println(rs);
                System.out.println(socket.getRemoteSocketAddress());
                System.out.println("--------------------");
            }catch(Exception e){
                System.out.println(socket.getRemoteSocketAddress() + "离线了!");
                dis.close();
                socket.close();
                break;
            }
        }
    }
}
